<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/7/25
 * Time: 20:59:51
 * 财务审核管理
 */
class goldCheck extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('check_model');
        $this->load->model('procurement_model');
//        $this->load->model('user_model');
        $this->load->model('purchaseOrder_model');
        $this->load->model('goldCheck_model');
        $this->load->model('stockPut_model');

    }



    //审核首页
    public function index()
    {
        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量

        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        if (!isset($_GET['type'])) {
            $_GET['type'] = 1;
        }

        $type = isset($_GET['type']) ? $_GET['type'] : 2;
        if ($type == 1) {
            // 默认进货单的数据
            $data = $this->purchaseOrder_model->page('purchaseOrder', 0, $page_num, $start_position); 
			
            foreach ($data[0] as $k => $v) {
                $data[0][$k]['payInfo'] = json_decode($v['payInfo'], true);
            }

            foreach ($data[0] as $k2 => $v2) {
                $last = end($data[0][$k2]['payInfo']);

                switch ($last['prepay']) {
                    case 0.05:
                        $last['prepay'] = '预付款5%';
                        break;
                    case 0.10:
                        $last['prepay'] = '预付款10%';
                        break;
                    case 0.15:
                        $last['prepay'] = '预付款15%';
                        break;
                    case 0.2:
                        $last['prepay'] = '预付款20%';
                        break;
                    case 0.3:
                        $last['prepay'] = '预付款30%';
                        break;
                    case 0.4:
                        $last['prepay'] = '预付款40%';
                        break;
                    case 0.5:
                        $last['prepay'] = '预付款50%';
                        break;
                    case 0.6:
                        $last['prepay'] = '预付款60%';
                        break;
                    case 0.7:
                        $last['prepay'] = '预付款70%';
                        break;
                    case 0.8:
                        $last['prepay'] = '预付款80%';
                        break;
                    case 1:
                        $last['prepay'] = '预付款100%';
                        break;
                }

                $data[0][$k2]['nowPrepay'] = $last['prepay'];
                $data[0][$k2]['nowPayMan'] = $last['payMan'];

                unset($data[0][$k2]['payInfo']);
            }

            if ($data[0]) {
                $viewData = [
                    'list' => $data,  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条

                ];
                $this->load->view('admin/goldCheck/list', $viewData);
            } else {
                $viewData = [
                    'list' => '',  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条
                ];
                $this->load->view('admin/goldCheck/list', $viewData);
            }
        } elseif ($type == 2) {
            //已审核项目的数据,从goldCheck数据库中读取
            $data2 = $this->goldCheck_model->page('goldCheck', 0, $page_num, $start_position);

//            __format($data2);

            if ($data2[0]) {
                $viewData = [
                    'list2' => $data2,  //这里得到从数据库中的当页数据
                    'totalNum' => $data2['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条

                ];
                $this->load->view('admin/goldCheck/list', $viewData);
            } else {
                $viewData = [
                    'list2' => '',  //这里得到从数据库中的当页数据
                    'totalNum' => $data2['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条
                ];
                $this->load->view('admin/goldCheck/list', $viewData);
            }
        }


    }


    // 财务确认支付功能
    public function purchaseConfirmPay()
    {

        //确认付款之后获取到onStatus为1

        //然后更新status为2   onStatus为2

        //然后采购单那边在提交,status为1,  在确认付款的时候为onstatus+1

//        这边更新status和onstatus两个字段

        $onStatus = $this->input->get('onStatus'); //当前状态
        $status = $this->input->get('status');  //上一次的状态值
        $prepay = $this->input->get('prepay');   //当前的付款比例

        if(strpos($prepay,'100')){
            $data = [
                'status' => 5, //2
                'onStatus' => $status,
            ];
        }else{
            $data = [
                'status' => $onStatus + 1, //2
                'onStatus' => $status,
            ];
        }

        $where = [
            'id' => $this->input->get('id')
        ];


        //是否会有添加失败的情况?
        $resultData = $this->purchaseOrder_model->edit($data, $where);

        //查询进货单的信息
        $purchaseInfo = $this->purchaseOrder_model->getByFieldQueryData('*',$where,0);




        if ($resultData) {


            //在更新成功之后，才添加进货信息,并且，状态是 5 的
            if($data['status'] == 5){
                $stockPut = [
                    'type' => '采购需求单',
                    'stockPutCode' => 'RK' . date('Ymd') . rand(100, 999),
                    'purchaseCode' => $purchaseInfo['purchaseCode'], //进货单编号
                    'createMan' => $_COOKIE['nickname'], //这个没法获取
                    'supplier' => $purchaseInfo['supplier'],
                    'payInfo' => $purchaseInfo['payInfo'], //付款信息,
                    'logisticsInfo' => $purchaseInfo['logisticsInfo'],
                    'content' => $purchaseInfo['content'],
                    'status' => 0,
                    'createTime' => time(),
                    'attribute' => $purchaseInfo['attribute'],
                    'goodsMessage' => $purchaseInfo['goodsMessage'],

                    'source'=> $purchaseInfo['source'], //进入单所属项目
                ];

                $this->stockPut_model->add($stockPut);
            }

            redirect('/admin/goldCheck/index?page=1&type=1');
        } else {
            redirect('home/404');
        }
    }

    public function detail()
    {
        //根据传过来的id,查询需求单/项目单 数据页面
        $arg = [
            'id' => $_GET['id']
        ];
        $data['list'] = $this->procurement_model->getByFieldQueryData('*', $arg, 0);
//        $data['list'] = json_encode($data['list']['goodsMessage'],true);

        //如果不存在直接跳转到404页面
        if (!$data['list']) {
            redirect('/home/error');
        }


        //进货单另外读取
        $this->load->view('/admin/check/detail', $data);

    }


    /**
     * 选择不同状态,获取数据
     * 已审核为1,待审核为0
     */
    private function getStatus()
    {

        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量

        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        //如果为1表示待审核
        if ($_POST['status'] == 1) {
            $checkWhere = [
                'status' => 1,
                //如果需要显示自己审核通过的数据,则加上这个条件
//                'checker' => '李总'
            ];
        } else {
            $checkWhere = [
                'status !=' => 1,
                //更换成动态名称

//                'checker' => '李总'
            ];
        }

        $data = $this->purchaseOrder_model->page('purchaseOrder', $checkWhere, $page_num, $start_position); //limit查询的数据

        //删除状态为0的数据,此时表示待提交
        foreach ($data[0] as $k3 => $v3) {
            if ($v3['status'] == 0) {
                unset($data[0][$k3]);
            }
        }

        //删除完之后重装数组,保持key不变
        sort($data[0]);

        /*
        id,status,purchaseCode,type,name,createTime,source,attribute,nowPrepay
        */
//        foreach ($data[0] as $k3=>$v3){
//            $data[0]['']
//        }

        //如果有数据的情况下
        if ($data[0]) {

            foreach ($data[0] as $k => $v) {
                $data[0][$k]['payInfo'] = json_decode($v['payInfo'], true);
            }

            foreach ($data[0] as $k2 => $v2) {
                $last = end($data[0][$k2]['payInfo']);

//                    __logInfo('debug',json_encode($last,JSON_UNESCAPED_UNICODE));

                switch ($last['prepay']) {
                    case 0.05:
                        $last['prepay'] = '预付款5%';
                        break;
                    case 0.10:
                        $last['prepay'] = '预付款10%';
                        break;
                    case 0.15:
                        $last['prepay'] = '预付款15%';
                        break;
                    case 0.2:
                        $last['prepay'] = '预付款20%';
                        break;
                    case 0.3:
                        $last['prepay'] = '预付款30%';
                        break;
                    case 0.4:
                        $last['prepay'] = '预付款40%';
                        break;
                    case 0.5:
                        $last['prepay'] = '预付款50%';
                        break;
                    case 0.6:
                        $last['prepay'] = '预付款60%';
                        break;
                    case 0.7:
                        $last['prepay'] = '预付款70%';
                        break;
                    case 0.8:
                        $last['prepay'] = '预付款80%';
                        break;
                    case 1:
                        $last['prepay'] = '预付款100%';
                        break;
                }

                $data[0][$k2]['nowPrepay'] = $last['prepay'];
                $data[0][$k2]['nowPayMan'] = $last['payMan'];

                unset($data[0][$k2]['payInfo']);
            }

//                echo '<pre>';
//            echo json_encode($data,JSON_UNESCAPED_UNICODE);
//
//                die();

            if ($data[0]) {
                $viewData = [
                    'list' => $data,  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条

                ];

            } else {

                $viewData = [
                    'list' => '',  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条
                ];

            }

//            print_r($viewData);
//            die();

            echo json_encode($viewData, JSON_UNESCAPED_UNICODE);


//            $this->load->view('admin/check/list',$viewData);

        } else {
            $viewData = [
                'list' => '',  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum' => $page_num //每页显示多少条
            ];

            echo json_encode($viewData);

//            $this->load->view('admin/check/list',$viewData);

        }

    }


}